﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using BPM.Web;
using BPM.ViewModel;
using BPM.Service;
using BPM.Common;
using BPM.Entity;

namespace BPM.Controllers
{
	public class MaterialInDeliveryController : InternalViewController
	{
		/// <summary>
		/// 物料入库
		/// </summary>
		/// <returns></returns>
		[HttpGet]
		public ActionResult MaterialInDelivery()
		{
			return View();
		}

		/// <summary>
		/// 
		/// </summary>
		/// <param name="allMaterials">
		/// id=amount,id=amount,id=amount...
		/// </param>
		/// <returns></returns>
		public ActionResult AJAX_GetCorrespondingPurchaseOrders(
			string allMaterials
			)
		{
			var materialsAmount = ReadAllMaterials(allMaterials);
			var activeOrders = new PurchaseOrderService().GetActiveOrders()
				.Where(o => o.IsRequiredMaterials(materialsAmount))
				.Select(o => new Client_Order()
				{
					id = o.Id,
					barcode = o.Barcode.Number,
					createdTime = o.CreatedTime.ToString(),
					creator = o.Creator.RealName,
					//剩余的物料数
					amount = o.GetLeftAmout()
				}).ToList();

			return Json(activeOrders);
		}

		/// <summary>
		/// 
		/// </summary>
		/// <param name="allMaterials">
		/// id=amount,id=amount,id=amount...
		/// </param>
		/// <param name="purchaseOrderId"></param>
		/// <returns></returns>
		public ActionResult AJAX_SubmitMaterials(
			string allMaterials,
			int purchaseOrderId
			)
		{
			var materialsAmount = ReadAllMaterials(allMaterials);

			var res = new MaterialInDeliveryService()
				.MaterialIn(materialsAmount, purchaseOrderId, this.CurrentUser);

			return Json(res);
		}

		public ActionResult AJAX_GetMaterial(string barcode)
		{
			var svc = new MaterialDefinitionService();
			var m = svc.GetByBarcode(barcode);
			if (m != null)
			{
				var message = JsonString(new
				{
					id = m.Id,
					name = m.Name,
					amount = 1,
					image = Url.Content(m.ImagePath),
					barcode = barcode,
				});

				return Json(new Result(true, message));
			}
			else
			{
				return Json(new Result("没有找到这个条码的物料！"));
			}
		}

		/// <summary>
		/// return: id:amount
		/// </summary>
		/// <param name="allMaterials"></param>
		/// <returns></returns>
		public static Dictionary<int, int> ReadAllMaterials(string allMaterials)
		{
			var dic = new Dictionary<int, int>();//id:amount
			var items = allMaterials.Split(",".ToCharArray(), StringSplitOptions.RemoveEmptyEntries);
			foreach (var item in items)
			{
				var values = item.Split("=".ToCharArray(), StringSplitOptions.RemoveEmptyEntries);
				var id = ParsePositiveInt(values[0]);
				var amount = ParsePositiveInt(values[1]);
				if (id > 0 && amount > 0)
				{
					dic[id] = amount;
				}
			}
			return dic;
		}

		/// <summary>
		/// 客户端使用的Order实体
		/// </summary>
		private class Client_Order
		{
			public int id { get; set; }
			public string barcode { get; set; }
			public string createdTime { get; set; }
			public string creator { get; set; }
			public int amount { get; set; }
		}

		/// <summary>
		/// 等待审核的物料入库请求列表
		/// </summary>
		/// <returns></returns>
		public ActionResult PendingMaterialInDeliveryList()
		{
			return View();
		}

		/// <summary>
		/// 等待审核的物料入库请求
		/// </summary>
		/// <returns></returns>
		public ActionResult PendingMaterialInDelivery()
		{
			return View();
		}
	}

	public class MaterialInDeliveryControllerActions : ControllerDefinition<MaterialInDeliveryController>
	{
		public readonly string MaterialInDelivery = "MaterialInDelivery";
		public readonly string PendingMaterialInDeliveryList = "PendingMaterialInDeliveryList";
		public readonly string PendingMaterialInDelivery = "PendingMaterialInDelivery";
	}
}